<template>
	<div class="app-container">
		<jk-operation-card ref="box1" class="margin-bottom-10">
			<div class="flex-between-center-wrap">
				<div class="flex-start-center-wrap">
					<el-radio-group v-model="viewType" size="small" class="item-gap">
						<el-radio-button label="listView">
							<span class="el-icon-c-scale-to-original"></span>
							<span>列表</span>
						</el-radio-button>
						<el-radio-button label="chartView">
							<span class="el-icon-s-data"></span>
							<span>图表</span>
						</el-radio-button>
					</el-radio-group>
				</div>
				<div class="flex-end-center-wrap">
					<jk-date-picker v-model="searchParams.belongDate" placeholder="日期" :picker-options="pickerOptions" size="small" style="width: 140px;" class="margin-5-0 item-gap" clearable />
					<el-select v-model="searchParams.machineCode" clearable placeholder="机器" size="small" class="search-bar-item-size1 margin-5-0 item-gap">
						<el-option v-for="item in machineList" :key="item.code" :label="item.name" :value="item.code" />
					</el-select>
					<jk-search-button class="" @click="onSearchBtn"></jk-search-button>
				</div>
			</div>
		</jk-operation-card>
		<jk-card>
			<component
				:is="viewType"
				ref="exportTotalUse"
				:loading="tableLoading"
				:search-params="searchParams"
				:table-data="tableData"
				:machine-code="machineCode"
				:height="tableHeight + 20"
				@getListRequest="getListRequest"
			></component>
			<!-- 分页组件 -->
			<footer ref="box2" class="flex-end-center margin-top-10">
				<jk-page
					:page-index="searchParams.pageNo"
					:page-sizes="searchParams.pageSizes"
					:page-size="searchParams.pageSize"
					:total="searchParams.count"
					@on-change="onPage"
				/>
			</footer>
		</jk-card>
	</div>
</template>

<script>
    import calHeight from '/src/mixins/cal-table-height';
    import { formatDate, formatTime, pageConfig } from '/src/utils/jk/jk-tool';
    import permissionMixin from '@/mixins/permission-mixin';
    import listView from './components/list-view';
    import chartView from './components/chart-view';
    import {
        iotCollectDataCountPage
    } from '../../../api/production/frontGroupYieldStatistics';
    import { systemUserColumnConfigGet } from '../../../api/system/column-config';
    import { productionPrdFiberReportGetPrdFiberStatistics } from '../../../api/production/heteroCeleReportForms';
    import { machinemanagerMachinePage } from '../../../api/machine/machineArchives';

    export default {
        name: 'HeteroCeleReportForms',
        mixins: [permissionMixin, calHeight],
        components: {
            listView,
            chartView
        },
        data() {
            return {
                pickerOptions: {
                    disabledDate(time) {
                        return time.getTime() > Date.now();
                    }
                },
                tableColumnList: [],
                prefix: '',
                viewType: 'listView',
                machineList: [],
                machineCode: '',
                dataId: null,
                tableLoading: false,
                exportLoading: false,
                tableData: [],
                searchParams: {
                    count: 0,
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize,
                    machineCode: '',
                    belongDate: '',
                    workshopId: null,
                    harder: ''
                }
            };
        },
        methods: {
            onPage(e, size) {
                this.searchParams.pageNo = e;
                this.searchParams.pageSize = size;
                this.getListRequest();
            },
            /** 查询列表 */
            getListRequest() {
                this.tableLoading = true;
                productionPrdFiberReportGetPrdFiberStatistics(this.searchParams).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        res.data.res ? res.data.res.forEach(x => {
                            if (x.belongTime) {
                                x.belongTime = Number(x.belongTime);
                            }
                        }) : [];
                        this.tableData = res.data.res;
                        if (this.searchParams.machineCode === 'YX001') {
                            this.tableData.forEach(x => {
                                x.machineList.forEach(y => {
                                    x.YX001KeJian = y.spurtCount3;
                                    x.YX001ZiWai = y.spurtCount4;
                                });
                            });
                        } else if (this.searchParams.machineCode === 'YX002') {
                            this.tableData.forEach(x => {
                                x.machineList.forEach(y => {
                                    x.YX002KeJian = y.spurtCount3;
                                    x.YX002ZiWai = y.spurtCount4;
                                });
                            });
                        } else {
                            this.tableData.forEach(x => {
                                // x.danKeJian = x.machineList.reduce((accumulator, currentValue) => {
                                //     return accumulator + Number(currentValue.spurtCount3);
                                // }, 0);
                                // x.danZiWai = x.machineList.reduce((accumulator, currentValue) => {
                                //     return accumulator + Number(currentValue.spurtCount4);
                                // }, 0);
                                x.machineList.forEach(y => {
                                    if (y.machineCode === 'YX002') {
                                        x.YX002KeJian = y.spurtCount3;
                                        x.YX002ZiWai = y.spurtCount4;
                                    }
                                });
                                x.machineList.forEach(y => {
                                    if (y.machineCode === 'YX001') {
                                        x.YX001KeJian = y.spurtCount3;
                                        x.YX001ZiWai = y.spurtCount4;
                                    }
                                });
                            });
                        }
                        this.tableData.forEach(x => {
                            x.keJian = x.machineList.reduce((accumulator, currentValue) => {
                                return accumulator + Number(currentValue.spurtCount3);
                            }, 0);
                            x.ziWai = x.machineList.reduce((accumulator, currentValue) => {
                                return accumulator + Number(currentValue.spurtCount4);
                            }, 0);
                            x.machineList.forEach(y => {
                                if (y.machineCode === 'YX001') {
                                    x.machine = {
                                        'name': y.machineName,
                                        'spurtCount1': y.spurtCount1,
                                        'spurtCount2': y.spurtCount2,
                                        'spurtCount3': y.spurtCount3,
                                        'spurtCount4': y.spurtCount4
                                    };
                                } else if (y.machineCode === 'YX002') {
                                    x.machine2 = {
                                        'name': y.machineName,
                                        'spurtCount1': y.spurtCount1,
                                        'spurtCount2': y.spurtCount2,
                                        'spurtCount3': y.spurtCount3,
                                        'spurtCount4': y.spurtCount4
                                    };
                                } else {
                                    x.machine2 = {};
                                }
                            });
                        });
                        this.searchParams.count = res.data.count;
                    }
                });
            },
            /** 搜索按钮操作 */
            onSearchBtn() {
                this.searchParams.pageNo = 1;
                this.searchParams.count = 1;
                this.machineCode = this.searchParams.machineCode;
                this.getListRequest();
            },
            /** 导出按钮操作 */
            onReportBtn() {
                const params = this.searchParams ? JSON.parse(JSON.stringify(this.searchParams)) : {};
                params.pageNo = 1;
                params.pageSize = 500;
                this.$modal.confirm('是否确认导出所有数据数据项?').then(() => {
                    this.exportLoading = true;
                }).then(() => {
                    this.exportLoading = false;
                    return iotCollectDataCountPage(params).then(res => {
                        if (res.data.status === 200) {
              import('@/vendor/Export2Excel').then(excel => {
                const tHeader = this.tableColumnList.map(x => x.title);
                const filterVal = this.tableColumnList.map(x => x.keyName);
                const list = JSON.parse(JSON.stringify(res.data.res)).map(item => {
                  return item;
                });
                // 合计项
                const totalItem = {};
                filterVal.forEach((x, idx) => {
                  totalItem[x] = this.$refs.exportTotalUse.$refs.frontGroupSalaryMonthReportTable.$refs.xTable.getTableData().footerData[0][idx];
                });
                list.push(totalItem);
                const data = list.map(v => filterVal.map(j => {
                  return v[j];
                }));
                excel.export_json_to_excel({
                  header: tHeader,
                  data,
                  filename: `${this.$route.meta.title}`,
                  autoWidth: true,
                  bookType: 'xlsx'
                });
              });
                        }
                    });
                }).catch(() => {});
            },
            getMenuColumnListRequest() {
                return systemUserColumnConfigGet({ path: 'HeteroCeleReportForms', filterShow: true }).then(res => {
                    if (res.data.status === 200) {
                        this.tableColumnList = res.data.res.columnConfigList && res.data.res.columnConfigList.length ? res.data.res.columnConfigList[0].itemList : [];
                    }
                });
            },
            getMachineListRequest() {
                const params = {
                    pageNo: 1,
                    pageSize: 100,
                    processId: 39,
                    nameOrCode: '',
                    typeCode: '',
                    workshopId: ''
                };
                return machinemanagerMachinePage(params).then(res => {
                    if (res.data.status === 200) {
                        this.machineList = res.data.res || [];
                    }
                });
            },
            async getDependentData() {
                this.searchParams.belongDate = formatDate(Date.now());
                await this.getMenuColumnListRequest();
                await this.getListRequest();
                await this.getMachineListRequest();
            }
        },
        mounted() {
            this.getDependentData();
        }
    };
</script>
<style scoped>
</style>
